package com.kpijavatraining.module.io;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import com.kpijavatraining.module.utils.WrapperList;
import com.kpijavatraining.module2.question.Question;
import com.kpijavatraining.module2.question.QuestionManyAnswers;
import com.kpijavatraining.module2.question.QuestionOneAnswer;
import com.kpijavatraining.module2.question.QuestionStringAnswer;

class SimpleHandler implements ContentHandler{

	private WrapperList<Question> list = new WrapperList<>();
	private List<String> answers = new ArrayList<>();
	private List<Integer> correctAnswers = new ArrayList<>();
	private String name;
	private String question;
	private String correctAnswer;
	private String type;
	private boolean isElement;
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		
		if(localName.equals("question")){
				type = atts.getValue(1);
				question = atts.getValue(0);
		}		
			name = new String(localName);
			
			if(localName.equals("answer") || localName.equals("correctAnswer")){
				isElement = true;
			}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equals("question")){
			switch(type){
			case "one"   : createQuestionOneAnswer();break;
			case "many"  : createQuestionManyAnswers();break;
			case "string": createQuestionString();break;
			}
			answers.clear();
			correctAnswers.clear();	
		}			
		isElement = false;
	}

	private void createQuestionOneAnswer(){
		String[] answers = this.answers.toArray(new String[this.answers.size()]);
		int correct = this.correctAnswers.get(0);
		QuestionOneAnswer question = new QuestionOneAnswer(this.question,answers,correct);
		list.add(question);
	}
	
	
	private void createQuestionManyAnswers(){
		String[] answers = this.answers.toArray(new String[this.answers.size()]);
		Integer[] correct = correctAnswers.toArray(new Integer[correctAnswers.size()]);
		QuestionManyAnswers question = new QuestionManyAnswers(this.question,answers,correct);
		list.add(question);
	}
	
	private void createQuestionString(){
		QuestionStringAnswer question = new QuestionStringAnswer(this.question,correctAnswer);
		list.add(question);
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if(isElement){
			String answer = new String(ch,start,length);
			switch(name){
			case "answer" : answers.add(answer);break;
			case "correctAnswer" : setCorrectAnswer(answer);break;
			}
		}
	}
	
	private void setCorrectAnswer(String answer){
			
		if(type.equals("string")){
			correctAnswer = answer;
		}else{
			correctAnswers.add(new Integer(Integer.parseInt(answer)));
		}
	}
	
	public WrapperList<Question> getQuestions(){
		return list;
	}
	
	
	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub	
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		
	}
	
}
